package com.mumulx.check.service;

import com.mumulx.check.redis.RedisUtil;
import com.mumulx.check.redis.entity.PartTestBoxed;
import com.mumulx.check.redis.entity.PartTestReturnBoxed;
import com.mumulx.check.redis.util.FastDfsClientUtil;
import com.mumulx.check.server.ExecuteChecker;
import nxopen.NXException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/*
 *  Created by IntelliJ IDEA.
 *  User: 木木
 *  Date: 2020/11/4
 *  Time: 17:06
 */
@Service
public class CheckPartService {

    @Value("${nx.partdownloadpath}")
    private String partDownloadPath;
    @Value("${nx.clxfid}")
    private int clxfid;
    @Resource
    ExecuteChecker executeChecker;

    @Resource
    RedisUtil redisUtil;

    public void checkpart(PartTestBoxed partTestBoxed) throws IOException, NXException {

        /*1.从fastdfs中下载文件
        * key: srfid
        * value: prtFileSavePath
        * **/
        Map<Integer, String> parts = partTestBoxed.getParts();

        //文件在本机中的存储位置，规则为：存储位置/用户id/测试记录id
        /*这边要特别注意文件路径的斜杠的个数\\
        * 最终的样式应该为：D:\workplace\nxdownload\9\122\285
        * */
        String filePath=partDownloadPath+partTestBoxed.getUserId()+"\\"+partTestBoxed.getTestRecordId()+"\\";
        /*本地文件的存储位置
        * key:srfid
        * value:下载的文件在本地的存储位置但不带文件后缀  D:\workplace\nxdownload\9\122\285\model1
        * */
        Map<Integer, String> partSaveUrl = executeChecker.downloadPart(parts, filePath);


        /*2.执行测试**/
        int execute=executeChecker.execute(partSaveUrl, partTestBoxed.getRules(), filePath);
        /*3.获取文件预览图并上传预览图--返回的是预览图在fastdfs上的存储位置
        * key: sfrid
        * value: prt预览图片xx.png在fastdfs中的存储的位置
        */
        Map<Integer, String> pngResultFiles = executeChecker.getAndUploadPrtPreviewImages(partSaveUrl);
        /*4.上传结果文件：返回结果为结果文件.xml在fastdfs中保存的位置
        * key:srfid
        * value: 测试结果文件xx.xml 在fastdfs中的存储位置
        * */
        Map<Integer, String> xmlResultFiles = executeChecker.uploadResultFile(partSaveUrl,filePath);

        /*将测试结果一并返回*/
        //resultFiles.put(-1, execute + "");
        /*5.redis存储结果：用户id_测试记录id_result*/
        //5.1 包装结果
        List<PartTestReturnBoxed> list = new ArrayList<>();
        String xslResultFile = xmlResultFiles.get(clxfid);

        for (Integer integer : parts.keySet()) {
            PartTestReturnBoxed prb = new PartTestReturnBoxed();
            prb.setSfrID(integer);
            prb.setXmlFilePath(xmlResultFiles.get(integer));
            prb.setPngFilePath(pngResultFiles.get(integer));
            prb.setXslFilePath(xslResultFile);
            list.add(prb);
        }


        //存储测试结果
        redisUtil.set(partTestBoxed.getUserId() + "_" + partTestBoxed.getTestRecordId() + "_result",list,10, TimeUnit.HOURS);
        //更改测试进度为  还有0个文件需要检测
        redisUtil.update(partTestBoxed.getUserId() + "_" + partTestBoxed.getTestRecordId(),0);

        File directory = new File(filePath);
        /*5.删除结果文件及其prt
        *
        * 暂时不删除---程序部署的时候可以删除
        * */
        //executeChecker.deleteAllFiles(directory);

    }



}
